/**
 * file
 */
package com.tdkj.action.front.member.financial;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.financial.BeanTrading;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.financial.IBeanTradingService;

/**
 * 
 * FileName: FreeTradingAction.java
 * 
 * @Description: TODO自由交易
 * 
 *               All rights Reserved, Designed By ZTE-ITS
 * 
 *               Copyright: Copyright(C) 2010-2011
 * 
 *               Company 天成科技有限公司
 * 
 * @author: 李兴建
 * 
 * @version V1.0
 * 
 *          Createdate: 2012-5-27 下午02:14:58
 * 
 * 
 * 
 *          Modification History:
 * 
 *          Date Author Version Discription
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 *          
 *          ----------------------------------------------------------------------
 *          -------------
 * 
 *          2012-5-27 李兴建 1.0 1.0
 * 
 *          Why & What is modified: <修改原因描述>
 */
public class TradingAction extends BaseAction {
	private static final long serialVersionUID = -3346624240604393484L;
	private BeanTrading bean;
	private List<BeanTrading> myList;
	private List<BeanTrading> historyList;
	private BeanUser currUser;
	
	private IBeanTradingService tradingService;

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		currUser = this.getCurrentUser();
		if (null == bean) {
			bean = new BeanTrading();
		}
		// 我在市场上的货物
		Criteria criteria = this.createCriteria(BeanTrading.class);
		criteria.add(Restrictions.eq("user", currUser));
		criteria.add(Restrictions.eq("state", 0));
		criteria.addOrder(Order.desc("dateTime"));
		myList = criteria.list();
		
		//历史买卖
		String hqlHistory = " from " + BeanTrading.class.getName() + " as bean where (bean.user.id = '"+currUser.getId()+"' or bean.toUser.id = '"+currUser.getId()+"') order by bean.dateTime desc" ;
		Query queryHistory = this.createQuery(hqlHistory);
		
		historyList = queryHistory.list();
		
		// 市场上的货物(有分页)
		Criteria criteria2 = this.createCriteria(BeanTrading.class);
		criteria2.add(Restrictions.eq("state", 0));
		criteria2.addOrder(Order.desc("dateTime"));// 对状态进行排序
		this.queryByPage(criteria2, "freeTradingAction.action", 10);
		return super.execute();
	}

	public String save() throws Exception {
		tradingService.create(this.getCurrentUser().getId(), bean.getProvide(), bean.getDemand());
		return execute();
	}

	public String cancel() throws Exception {
		tradingService.cancel(bean.getId());
		return execute();
	}
	
	public String transaction() throws Exception {
		tradingService.transaction(bean.getId(),this.getCurrentUser().getId());
		return execute();
	}

	public BeanTrading getBean() {
		return bean;
	}

	public void setBean(BeanTrading bean) {
		this.bean = bean;
	}

	public List<BeanTrading> getMyList() {
		return myList;
	}

	public void setMyList(List<BeanTrading> myList) {
		this.myList = myList;
	}

	public BeanUser getCurrUser() {
		return currUser;
	}

	public void setCurrUser(BeanUser currUser) {
		this.currUser = currUser;
	}

	public List<BeanTrading> getHistoryList() {
		return historyList;
	}

	public void setHistoryList(List<BeanTrading> historyList) {
		this.historyList = historyList;
	}

	@Resource
	public void setTradingService(IBeanTradingService tradingService) {
		this.tradingService = tradingService;
	}

}
